package cc.rengu.igas.channel.nucc.core.service.trans;

import cc.rengu.igas.channel.nucc.common.constant.NuccParamConstant;
import cc.rengu.igas.channel.nucc.common.constant.NuccTreeNodeConstant;
import cc.rengu.igas.channel.nucc.common.enums.*;
import cc.rengu.igas.channel.nucc.facade.request.SignStateQueryRequest;
import cc.rengu.igas.channel.nucc.facade.request.SignStateQueryAcqRequest;
import cc.rengu.igas.channel.nucc.facade.response.SignStateQueryResponse;
import cc.rengu.igas.channel.nucc.facade.response.SignStateQueryAcqResponse;
import cc.rengu.igas.channel.nucc.core.realize.impl.NuccSeqNoManageImpl;
import cc.rengu.igas.channel.nucc.core.realize.NuccSeqNoManage;
import cc.rengu.igas.channel.nucc.core.realize.NuccService;
import cc.rengu.igas.channel.nucc.core.realize.impl.NuccServiceImpl;
import cc.rengu.oltp.service.base.BizBaseService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.NonFinanceTxnMapper;
import cc.rengu.oltp.service.common.dao.SignAcctInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.NonFinanceTxnMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.SignAcctInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.BinInfo;
import cc.rengu.oltp.service.common.entity.NonFinanceTxn;
import cc.rengu.oltp.service.common.entity.SignAcctInfo;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.RiskMonitorInfo;
import cc.rengu.oltp.service.realize.BinInfoService;
import cc.rengu.oltp.service.realize.impl.BinInfoServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;


/**
 * 协议查询申请服务
 */
public class EpccSignQueryApplyService extends BizBaseService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        SignStateQueryRequest request = new SignStateQueryRequest();
        ConvertUtil.convertOutput(request);

        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.FRONT_SEQ_NUM, request.getHeader().getTraceNo());
        return request;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        SignStateQueryRequest innerRequest = (SignStateQueryRequest) request;
        String instId = innerRequest.getHeader().getInstId();
        String oriTrxId = innerRequest.getOriTrxId();
        String oriTrxDtTm = innerRequest.getOriTrxDtTm();
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ISSR_ID, innerRequest.getInstgId());

        SignStateQueryAcqRequest acqRequest = new SignStateQueryAcqRequest();
        acqRequest.setInstgId(innerRequest.getInstgId());
        acqRequest.setOriSgnAcctIssrId(innerRequest.getOriSgnAcctIssrId());
        acqRequest.setOriSgnAcctTp(innerRequest.getOriSgnAcctTp());
        acqRequest.setOriTrxCtgy(innerRequest.getOriTrxCtgy());
        /*将原交易渠道流水转换为原网联通道流水*/
        NonFinanceTxnMapper txnMapper = new NonFinanceTxnMapperImpl();
        NonFinanceTxn orgNonFinanceTxn = txnMapper.selectNonFinanceTxnByFrontSeqNum(instId, oriTrxDtTm, oriTrxId, NuccParamConstant.TUNL_ID_NUCC, innerRequest.getHeader().getChanlId());
        if (null == orgNonFinanceTxn) {
            rglog.error("根据前端流水号[{}]查询原交易失败!", oriTrxId);
            throw new BizException(RespCodeEnum.ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        acqRequest.setOriTrxId(orgNonFinanceTxn.getChannelSeqNum());
        //原交易日期时间从原交易流水号获取
        String srcDateTime = acqRequest.getOriTrxId().substring(0, 8) + acqRequest.getOriTrxId().substring(12, 18);
        String trxDateTime = DateUtil.dateStringFormatConvert(srcDateTime, "yyyyMMddHHmmss", "yyyy-MM-dd'T'HH:mm:ss");
        acqRequest.setOriTrxDtTm(trxDateTime);
        acqRequest.setOriPyeeMrchntNo(innerRequest.getOriMrchntNo());
        NuccService nuccService = new NuccServiceImpl();
        SignStateQueryAcqResponse acqResponse = nuccService.signStateQuery(acqRequest);

        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(NuccParamConstant.IGAS_SYS_ID);
        if (!NuccParamConstant.EPCC_SUCCESS_CODE.equals(acqResponse.getSysRtnCd())) {
            rglog.error("调用网联接口协议查询失败，respCode=[{}],respDesc=[{}]", acqResponse.getSysRtnCd(), acqResponse.getSysRtnDesc());

            bizResponse.setRespCode(acqResponse.getSysRtnCd());
            bizResponse.setRespDesc(acqResponse.getSysRtnDesc());
        } else {
            SignStateQueryResponse response = new SignStateQueryResponse();
            response.setSgnNo(acqResponse.getSignInfo().getSgnNo());
            response.setSgnStat(acqResponse.getSgnSts());
            response.setSgnAcctId(acqResponse.getSignInfo().getSgnAcctId());
            response.setSgnAcctNm(acqResponse.getSignInfo().getSgnAcctNm());

            bizResponse.setRespCode(acqResponse.getBizStsCd());
            bizResponse.setRespDesc(acqResponse.getBizStsDesc());
            bizResponse.setResult(response);
        }
        return bizResponse;
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {
        SignStateQueryResponse signStateQueryResponse = (SignStateQueryResponse) response.getResult();
        ConvertUtil.convertInput(signStateQueryResponse);
    }

    @Override
    protected RiskMonitorInfo getRiskMonitorInfo() {
        return null;
    }
}
